package com.tang.thepath;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.AuthService;
import com.tang.thepath.service.HttpSubscriber;

public class PasswordActivity extends BaseActivity {

    private EditText editOldPassword;
    private EditText editNewPassword;
    private EditText editRePassword;

    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        editOldPassword = (EditText) findViewById(R.id.edit_old_password);
        editNewPassword = (EditText) findViewById(R.id.edit_new_password);
        editRePassword = (EditText) findViewById(R.id.edit_re_password);

        btnSave= (Button) findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = editOldPassword.getText().toString();
                String newPassword = editNewPassword.getText().toString();
                String rePassword = editRePassword.getText().toString();

                if(oldPassword.isEmpty()){
                    toast("旧密码不能为空");
                    return;
                }

                if(!newPassword.equals(rePassword)){
                    toast("两次密码不同");
                    return;
                }

                showLoading("数据提交中");

                RequestModel.ChangePasswordRequest request = new RequestModel.ChangePasswordRequest();
                request.password = newPassword;
                request.oldPassword = oldPassword;
                AuthService.changePassword(request).subscribe(new HttpSubscriber<RequestModel.IdResponse>() {
                    @Override
                    protected void onSuccess(RequestModel.IdResponse idResponse) {
                        dismisLoading();
                        toast("修改成功，请重新登录");
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismisLoading();
                        toast(e.getMessage());
                        super.onError(e);
                    }
                });

            }
        });
    }
}
